﻿// -----------------------------------------------------------------------
// <copyright company="www.3WayWebdesign.de">
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
// 
// <author>Michael Morbach</author>
// <email>Michael@3waywebdesign.de</email>
// <web>http://www.3waywebdesign.de</web>
// ----------------------------------------------------------------------

using System;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Tww.MAF.MAFEngine.Navigator;

namespace Tww.MAF.MAFEngine.Navigation.Browser
{
    public class Navigator : ViewModelBase, INavigator
    {
        #region  - Felder (privat) -

        private ICommand _closeNavigatorCommand;
        private bool _isCloseable;

        #endregion

        #region  - Konstruktoren -

        public Navigator(INavigationJournal journal)
        {
            if (journal == null)
            {
                throw new ArgumentNullException("journal");
            }

            NavigatorId = Guid.NewGuid();
            Journal = journal;

            this.RegisterEvents();
        }

        private void RegisterEvents()
        {
            // Navigating abfangen und Page Title anpassen
        }

        #endregion

        #region  - Indexer und Eigenschaften (öffentlich) -

        #endregion

        #region  - INavigator Members -

        /// <summary>
        ///     Gets or sets the navigator identifier.
        /// </summary>
        /// <value>
        ///     The navigator identifier.
        /// </value>
        public Guid NavigatorId { get; private set; }

        public string CurrentPageTitle
        {
            get { return this.Journal.CurrentEntry.DisplayName; }
        }

        public void Navigate(Uri destination, NavigationContext context, Action<NavigationResult> navigationCallback)
        {
            if (destination == null)
            {
                throw new ArgumentNullException("destination");
            }

            //if (this.Journal.CurrentEntry != null)
            //{
            //    this.Journal.CurrentEntry.
            //}
            //var currentLocation = this.Journal.CurrentEntry.NavigationLocation;
            //this.RaiseNavigating(context);
            //this.RaiseNavigated();
        }

        public event EventHandler<NavigationEventArgs> Navigating;

        public event EventHandler<NavigationEventArgs> Navigated;

        public event EventHandler<NavigationEventArgs> NavigationFailed;

        public event EventHandler<NavigatorClosingEventArgs> NavigatorClosing;

        public event EventHandler NavigatorClosed;

        public ICommand CloseNavigatorCommand
        {
            get
            {
                return this._closeNavigatorCommand ?? (this._closeNavigatorCommand = new RelayCommand(Close, CanClose));
            }
        }

        /// <summary>
        ///     Gets the journal.
        /// </summary>
        /// <value>
        ///     The journal.
        /// </value>
        public INavigationJournal Journal { get; set; }

        /// <summary>
        ///     Gets a value indicating whether [is closeable].
        /// </summary>
        /// <value>
        ///     <c>true</c> if [is closeable]; otherwise, <c>false</c>.
        /// </value>
        public bool IsCloseable
        {
            get { return this._isCloseable; }
            set
            {
                this._isCloseable = value;
                RaisePropertyChanged(() => IsCloseable);
            }
        }

        #endregion

        #region  - Methoden (öffentlich) -

        public void Navigate(INavigateableDestination to)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region  - Methoden (privat) -

        protected virtual bool CanClose()
        {
            return IsCloseable;
        }

        protected void RaiseNavigating(NavigationContext navigationContext)
        {
            EventHandler<NavigationEventArgs> handler = Navigating;
            if (handler != null)
            {
                handler(this, new NavigationEventArgs(navigationContext));
            }
        }


        protected void RaiseNavigated(NavigationContext navigationContext)
        {
            EventHandler<NavigationEventArgs> handler = Navigated;
            if (handler != null)
            {
                handler(this, new NavigationEventArgs(navigationContext));
            }
        }


        protected void RaiseNavigationFailed(NavigationContext navigationContext)
        {
            EventHandler<NavigationEventArgs> handler = NavigationFailed;
            if (handler != null)
            {
                handler(this, new NavigationEventArgs(navigationContext));
            }
        }

        protected virtual void OnNavigatorClosing(NavigatorClosingEventArgs eventArgs)
        {
            EventHandler<NavigatorClosingEventArgs> handler = NavigatorClosing;

            if (handler != null)
            {
                handler(this, eventArgs);
            }
        }

        protected virtual void OnNavigatorClosed()
        {
            EventHandler handler = NavigatorClosed;

            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }

        protected virtual void Close()
        {
            var closingEventArgs = new NavigatorClosingEventArgs();
            OnNavigatorClosing(closingEventArgs);

            if (!closingEventArgs.Cancel)
            {
                Cleanup();
                OnNavigatorClosed();
            }
        }

        #endregion
    }
}